package demo13;

import java.util.ArrayList;
import java.util.List;

/**
 * 子集
 * https://leetcode.cn/problems/TVdhkn/
 */

class Solution1 {
    List<List<Integer>> ret;
    List<Integer> path;
    int len;
    public List<List<Integer>> subsets(int[] nums) {
        len = nums.length;
        ret = new ArrayList<>();
        path = new ArrayList<>();
        dfs(nums,0);
        return ret;
    }

    private void dfs(int[] nums, int index) {
        // 出口
        if(index == len) {
            ret.add(new ArrayList<>(path));
            return;
        }
        // 不选
        dfs(nums,index + 1);
        // 选
        path.add(nums[index]);
        dfs(nums, index + 1);
        // 还原现场
        path.remove(path.size()-1);
    }
}
class Demo {
    public static void main(String[] args) {
        Solution1 solution1 = new Solution1();
        List<List<Integer>> subsets = solution1.subsets(new int[]{1, 2, 3});
        for (List<Integer> nums : subsets) {
            for (int num : nums) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}